package com.proj.hot.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by zangchenggang on 16/11/16.
 */
public class SystemUtil {
    /**
     * 获取系统当前时间
     *
     * @return 返回格式：2016年11月16日 11:28:23
     */
    public static String getSystemTime() {
        String pattern = "yyyy年MM月dd日 HH:mm:ss";// 输出的日期格式
        return new SimpleDateFormat(pattern).format(new Date(System
                .currentTimeMillis()));
    }
    /**
     * 返回到桌面
     *
     * @param context
     * @return
     */
    public static void returnDesktop(Context context) { //
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
    /**
     * finish()：结束当前Activity，不会立即释放内存。遵循android内存管理机制。
     *
     * @param activity
     */
    public static void finish(Activity activity) {
        activity.finish();
    }
    /**
     * killProcess()：结束当前组件如Activity，并立即释放当前Activity所占资源。(可用)
     */
    public static void killProcess() {
        android.os.Process.killProcess(android.os.Process.myPid());
    }
    /**
     * System.exit(0)：结束当前组件如Activity，并立即释放当前Activity所占资源。(可用)
     *
     * @param context
     * @param packageName
     */
    public static void exit(Context context, String packageName) {
        int currentVersion = Build.VERSION.SDK_INT;
        if (currentVersion > Build.VERSION_CODES.ECLAIR_MR1) {
            Intent startMain = new Intent(Intent.ACTION_MAIN);
            startMain.addCategory(Intent.CATEGORY_HOME);
            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(startMain);
            System.exit(0); // 结束当前组件如Activity，并立即释放当前Activity所占资源。
        } else {// android2.1
            ActivityManager am = (ActivityManager) context
                    .getSystemService(context.ACTIVITY_SERVICE);
            am.restartPackage(packageName);
        }
    }

    /**
     * @return 系统版本
     */
    public static String getSystemVersion() {
        return Build.VERSION.INCREMENTAL.substring(1);
    }

    /**
     * 系统的版本信息 版本信息里面还包括型号等信息
     */
    public String[] getVersion() {
        String[] version = { "null", "null", "null", "null" };
        String str1 = "/procersion";
        String str2;
        String[] arrayOfString;
        try {
            FileReader localFileReader = new FileReader(str1);
            BufferedReader localBufferedReader = new BufferedReader(
                    localFileReader, 8192);
            str2 = localBufferedReader.readLine();
            arrayOfString = str2.split("\\s+");
            version[0] = arrayOfString[2];// KernelVersion
            localBufferedReader.close();
        } catch (IOException e) {
        }
        version[1] = Build.VERSION.RELEASE;// firmware version
        version[2] = Build.MODEL;// model
        version[3] = Build.DISPLAY;// system version
        return version;
    }

    /**
     * android的总内存大小信息存放在系统的/proc/meminfo文件里面，可以通过读取这个文件来获取这些信息：
     */
    public static void testGetTotalMemory() {
        String str1 = "/proc/meminfo";
        String str2 = "";
        try {
            FileReader fr = new FileReader(str1);
            BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
            while ((str2 = localBufferedReader.readLine()) != null) {
               // Log("testGetTotalMemory", "---" + str2);
            }
        } catch (IOException e) {
        }
    }

    /**
     * android的总内存ram大小信息存放在系统的/proc/meminfo文件里面，可以通过读取这个文件来获取这些信息：
     */
    public static String getTotalMemory() {
        String str1 = "/proc/meminfo";
        String str2 = "";
        try {
            FileReader fr = new FileReader(str1);
            BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
            str2 = localBufferedReader.readLine();
            String str3[] = str2.split("MemTotal:|kB");
            return (str3[1].trim() + "KB");

        } catch (IOException e) {
        }
        return "KB";
    }

    /**
     * 获取当前剩余内存(ram)大小
     */
    public static long getAvailMemory(Context context) {
        ActivityManager am = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
        am.getMemoryInfo(mi);
        return mi.availMem;
    }

    /**
     * 获得机身内存rom总大小(转换单位)
     *
     * @return
     */
    public static String getRomTotalSize(Context context) {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long totalBlocks = stat.getBlockCount();
        return Formatter.formatFileSize(context, blockSize * totalBlocks);
    }

    /**
     * 总rom(未转换单位)
     *
     * @return 单位字节
     */
    public static long getRomBlockCount(Context context) {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long blockCount = stat.getBlockCount();
        return blockSize * blockCount;
    }

    /**
     * 获得机身可用内存 rom(转换单位)
     *
     * @return
     */
    public static String getRomAvailableSize(Context context) {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return Formatter.formatFileSize(context, blockSize * availableBlocks);
    }

    /**
     * 可用rom(未转换单位)
     *
     * @return 单位字节
     */
    public static long getRomAvailableBlocks1(Context context) {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return blockSize * availableBlocks;
    }
    /**
     * 获得机身可用内存 rom(转换单位)
     *
     * @return
     */
    public static String getRomAvailableSize1(Context context) {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return Formatter.formatFileSize(context, blockSize * availableBlocks);
    }

    /**
     * 可用rom(未转换单位)
     *
     * @return 单位字节
     */
    public static long getRomAvailableBlocks(Context context) {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return blockSize * availableBlocks;
    }

    /**
     * rom大小
     *
     * @return romInfo[0],[1]/rom总、rom可用
     */
    public static long[] getRomMemroy() {
        long[] romInfo = new long[2];
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        // Total rom memory
        romInfo[0] = blockSize * stat.getBlockCount();// rom总
        // Available rom memory
        romInfo[1] = blockSize * stat.getAvailableBlocks();// rom可用
        return romInfo;
    }

    /**
     * 存储单位转换
     *
     * @param context
     * @param number
     * @return
     */
    public static String formatFileSize(Context context, long number) {
        return Formatter.formatFileSize(context, number);
    }

    /**
     * 获得SD卡总大小
     *
     * @return
     */
    public static String getSDTotalSize(Context context) {
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long totalBlocks = stat.getBlockCount();
        return Formatter.formatFileSize(context, blockSize * totalBlocks);
    }

    /**
     * SD卡总大小
     *
     * @return 单位字节
     */
    public static long getSDBlockCount(Context context) {
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long blockCount = stat.getBlockCount();
        return blockSize * blockCount;
    }

    /**
     * sd卡剩余容量，即可用大小
     *
     * @return
     */
    public static String getSDAvailableSize(Context context) {
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return Formatter.formatFileSize(context, blockSize * availableBlocks);
    }

    /**
     * sd卡剩余容量，即可用大小
     *
     * @return 单位字节
     */
    public static long getSDAvailableBlocks(Context context) {
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return blockSize * availableBlocks;
    }

    /**
     * sdCard大小
     *
     * @return [0]、[1]/总、可用
     */
    public static long[] getSDCardMemory() {
        long[] sdCardInfo = new long[2];
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            File sdcardDir = Environment.getExternalStorageDirectory();
            StatFs sf = new StatFs(sdcardDir.getPath());
            long bSize = sf.getBlockSize();
            long bCount = sf.getBlockCount();
            long availBlocks = sf.getAvailableBlocks();

            sdCardInfo[0] = bSize * bCount;// 总大小
            sdCardInfo[1] = bSize * availBlocks;// 可用大小
        }
        return sdCardInfo;
    }

    // /**
    // * 电池电量 registerReceiver(batteryReceiver, new
    // * IntentFilter(Intent.ACTION_BATTERY_CHANGED));//要注册接收者
    // */
    // public static void getlevel() {
    // //监听器中的写法
    // BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
    // public void onReceive(Context context, Intent intent) {
    // int level = intent.getIntExtra("level", 0);
    // // level加%就是当前电量了
    // }
    // };
    // }

    /**
     * ? CPU信息 /procuinfo文件中第一行是CPU的型号，第二行是CPU的频率，可以通过读文件，读取这些数据！
     */
    public static String[] getCpuInfo() {
        String str1 = "/procuinfo";
        String str2 = "";
        String[] cpuInfo = { "", "" };
        String[] arrayOfString;
        try {
            FileReader fr = new FileReader(str1);
            BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
            str2 = localBufferedReader.readLine();
            arrayOfString = str2.split("\\s+");
            for (int i = 2; i < arrayOfString.length; i++) {
                cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " ";
            }
            str2 = localBufferedReader.readLine();
            arrayOfString = str2.split("\\s+");
            cpuInfo[1] += arrayOfString[2];
            localBufferedReader.close();
        } catch (IOException e) {
        }
        return cpuInfo;
    }

}
